﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TicTacToe
{
    public partial class WebFormTicTacToe : System.Web.UI.Page
    {
        private static char[,] table = new char[3, 3];
        private static char player = 'o';

        private bool CheckForWinner()
        {
            //check rows
            if ((table[0, 0] == table[0, 1]) && (table[0, 1] == table[0, 2]) && (table[0,0] == player))
            {
                return true;
            }
            if ((table[1, 0] == table[1, 1]) && (table[1, 1] == table[1, 2]) && (table[1, 0] == player))
            {
                return true;
            }
            if ((table[2, 0] == table[2, 1]) && (table[2, 1] == table[2, 2]) && (table[2, 0] == player))
            {
                return true;
            }
            //check cols
            if ((table[0, 0] == table[1, 0]) && (table[1, 0] == table[2, 0]) && (table[0, 0] == player))
            {
                return true;
            }
            if ((table[0, 1] == table[1, 1]) && (table[1, 1] == table[2, 1]) && (table[0, 1] == player))
            {
                return true;
            }
            if ((table[0, 2] == table[1, 2]) && (table[1, 2] == table[2, 2]) && (table[0, 2] == player))
            {
                return true;
            }
            //check diagonals
            if ((table[0, 0] == table[1, 1]) && (table[1, 1] == table[2, 2]) && (table[0, 0] == player))
            {
                return true;
            }
            if ((table[2, 0] == table[1, 1]) && (table[1, 1] == table[0, 2]) && (table[2, 0] == player))
            {
                return true;
            }

            return false;
        }

        private void NewGame()
        {
            table = new char[3, 3];
            player = 'o';
            this.cell11.ImageUrl = "blank.gif";
            this.cell12.ImageUrl = "blank.gif";
            this.cell13.ImageUrl = "blank.gif";
            this.cell21.ImageUrl = "blank.gif";
            this.cell22.ImageUrl = "blank.gif";
            this.cell23.ImageUrl = "blank.gif";
            this.cell31.ImageUrl = "blank.gif";
            this.cell32.ImageUrl = "blank.gif";
            this.cell33.ImageUrl = "blank.gif";
        }

        private void ChangeCell(ImageButton cell, char currentPlayer, int x,int y)
        {
            cell.ImageUrl = player + ".gif";
            table[x, y] = player;
            if (CheckForWinner())
            {
                if (currentPlayer == 'x')
                {
                    int points = int.Parse(this.playerXresult.Text);
                    points++;
                    this.playerXresult.Text = points.ToString();
                }
                else
                {
                    int points = int.Parse(this.playerOresult.Text);
                    points++;
                    this.playerOresult.Text = points.ToString();
                }
                NewGame();
            }
            else
            {
                switch (currentPlayer)
                {
                    case 'o':
                        player = 'x';
                        break;
                    case 'x':
                        player = 'o';
                        break;
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void cell11_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 0, 0);
        }

        protected void cell12_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 0, 1);
        }

        protected void cell13_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 0, 2);
        }

        protected void cell21_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 1, 0);
        }

        protected void cell22_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 1, 1);
        }

        protected void cell23_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 1, 2);
        }

        protected void cell31_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 2, 0);
        }

        protected void cell32_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 2, 1);
        }

        protected void cell33_Click(object sender, ImageClickEventArgs e)
        {
            ChangeCell((ImageButton)sender, player, 2, 2);
        }

        protected void buttonNewGame_Click(object sender, EventArgs e)
        {
            NewGame();
        }
    }
}